﻿using System;
using System.Collections.Generic;
using System.Linq;


namespace RaceList.activity.help
{
    /// <summary>
    ///     这里主要是封装了一些数据库中对Activity_Relay,Activity_SubRelay的操作
    /// </summary>
    public partial class RelayQuery
    {
        public static List<Activity_AwardUser> GetActivity_AwardUserList()
        {
            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
            List<Activity_AwardUser> list = db.Activity_AwardUser.ToList();
            return list;
        }

        /// <summary>
        ///     获得中了某个奖的所有用户
        /// </summary>
        /// <param name="AwardId"></param>
        /// <returns></returns>
        public static List<Activity_AwardUser> GetActivity_AwardUserList(int AwardId)
        {
            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
            List<Activity_AwardUser> list =
                db.Activity_AwardUser.Where(s => s.awardId == AwardId && s.isAward != null).ToList();
            return list;
        }

        /// <summary>
        ///     获取某天中某个奖的用户数量
        /// </summary>
        /// <param name="AwardId"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static List<Activity_AwardUser> GetActivity_AwardUserList(int AwardId, DateTime date)
        {
            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
            DateTime date1 = date.Date;
            DateTime date2 = date.Date.AddDays(1);
            List<Activity_AwardUser> list = GetActivity_AwardUserList(AwardId);

            list = list.Where(s => s.addTime >= date1 && s.addTime <= date2).ToList();

            return list;
        }

        public static Activity_AwardUser GetActivity_AwardUser(string winCode)
        {
            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
            Activity_AwardUser awardUser = db.Activity_AwardUser.FirstOrDefault(s => s.winCode == winCode);
            return awardUser;
        }

        public static Activity_AwardUser GetActivity_AwardUser(int relayId)
        {
            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
            Activity_AwardUser awardUser = db.Activity_AwardUser.FirstOrDefault(s => s.relayId == relayId);
            return awardUser;
        }

        public static bool ExistAwardUser(int relayId)
        {
            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
            return db.Activity_AwardUser.Count(s => s.relayId == relayId) > 0;
        }

        public static bool ExistAwardUserByWeixinUserId(int weixinUserId)
        {
            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
            return db.Activity_AwardUser.Count(s => s.Activity_Relay.WeixinUserId == weixinUserId) > 0;
        }

        public static Activity_AwardUser GetActivityAwardUser(int weixinUserId)
        {
            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
            return db.Activity_AwardUser.FirstOrDefault(s => s.Activity_Relay.WeixinUserId == weixinUserId);
        }

        /// <summary>
        ///     用户是否提交了自己的信息是否兑奖了（提交并提示中奖了等待工作人员联系）
        /// </summary>
        /// <param name="relayId"></param>
        /// <returns></returns>
        public static bool GetActivity_AwardUserIsAward(int relayId)
        {
            Activity_AwardUser awardUser = GetActivity_AwardUser(relayId);
            if (awardUser != null)
                return awardUser.isAward;
            return false;
        }

        public static string AddActivity_AwardUser(Activity_AwardUser awardUser)
        {
            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();

            db.Activity_AwardUser.InsertOnSubmit(awardUser);
            db.SubmitChanges();
            return awardUser.winCode;
        }

        public static string GuidTo16String(Guid ID)
        {
            long i = 1;
            foreach (byte b in ID.ToByteArray())
                i *= (b + 1);
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }

        //public static void SubmitUserInfo(string name, string phone, string winCode)
        //{

        //    var db = ConnHelper.ipowdb_suibian_web();
        //    var awardUser = GetActivity_AwardUser(winCode);

        //    if (awardUser == null)
        //        return;

        //    if (awardUser.isAward)
        //    {
        //        return;
        //    }
        //    else
        //    {
        //        var awards = GetAward();
        //        if (awards != null)
        //        {
        //            awardUser.name = name;
        //            awardUser.phone = phone;
        //            awardUser.isAward = true;
        //            awardUser.awardId = awards.Id;
        //        }
        //        db.SubmitChanges();
        //    }
        //}

        /// <summary>
        ///     修改名字和电话这些属性
        /// </summary>
        /// <param name="name"></param>
        /// <param name="phone"></param>
        /// <param name="winCode"></param>
        public static void UpdateUserInfo(string name, string phone, string winCode)
        {
            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
            Activity_AwardUser awardUser = db.Activity_AwardUser.Where(s => s.winCode == winCode).FirstOrDefault();

            if (awardUser == null)
                return;

            if (awardUser.isAward)
            {
            }
            awardUser.name = name;
            awardUser.phone = phone;
            db.SubmitChanges();
        }

        /// <summary>
        ///     修改奖品的数量
        ///     修改 awardId和isAward这两个属性
        /// </summary>
        /// <param name="award"></param>
        /// <param name="winCode"></param>
        public static void UpdateAwardInfo(Activity_Awards award, string winCode)
        {
            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
            Activity_AwardUser awardUser = db.Activity_AwardUser.Where(s => s.winCode == winCode).FirstOrDefault();
            Activity_Awards awardInDb =
                db.Activity_Awards.Where(s => s.Id == award.Id && s.status && s.isDel == false).Single();

            awardInDb.useredCount++;
            awardInDb.sumCount -= 1;
            db.SubmitChanges();

            if (awardUser != null)
            {
                awardUser.awardId = award.Id;
                awardUser.isAward = true;
                awardUser.AwardDate = DateTime.Now;
                db.SubmitChanges();
            }
        }

        /// <summary>
        ///     判断一个用户到底是中了什么奖
        ///     <param name="sponsorId">赞助商Id</param>
        /// </summary>
        public static Activity_Awards GetAward(int? sponsorId = null)
        {
            List<Activity_Awards> list = GetActivity_Awards(sponsorId);
            foreach (Activity_Awards item in list)
            {
                //1.判断总数
                int sumCount = GetActivity_AwardUserList(item.Id).Count;
                if (item.sumCount > sumCount) //这个总数是当前剩余奖品数
                {
                    //2.判断每天发放量
                    int dayCount = GetActivity_AwardUserList(item.Id, DateTime.Now).Count;
                    if (item.dayCount > dayCount)
                    {
                        return item;
                    }
                }
            }

            return null;
        }
    }
}